package com.xx.bysj.controller;


import com.alibaba.fastjson.JSON;
import com.xx.bysj.aop.LogAop;
import com.xx.bysj.aop.LogConsts;
import com.xx.bysj.dto.HnInformation;
import com.xx.bysj.dto.HnUser;
import com.xx.bysj.dto.UploadFileDto;
import com.xx.bysj.service.UserService;
import com.xx.bysj.utils.NumberUtils;
import com.xx.bysj.utils.Result;
import com.xx.bysj.utils.UploadUtils;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class UserController{

    @Autowired
    private UserService userService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @RequestMapping("/register")
    @LogAop(actionName = "用户注册",actionType = LogConsts.ACTION_ADD)
    public Result register(HnUser user, String type){
        return userService.register(user,type);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Result updateImager(@RequestParam("file") MultipartFile file, HttpServletRequest request, Authentication authentication){
        Result result = new Result();
        String imagePath = UploadUtils.upload(file,request).substring(7);
        System.out.println(imagePath);
        if (imagePath != null) {
            result.setCode(NumberUtils.TWO_HUNDRED);
            result.setMessage("上传成功");
            result.setData(imagePath);
        }else {
            result.setCode(NumberUtils.FIV_HUNDRED);
            result.setMessage("上传成功");
        }
        return result;
    }

    @RequestMapping("/updateUserInformation")
    @ResponseBody
    @LogAop(actionName = "用户信息更新",actionType = LogConsts.ACTION_UPDATE)
    public Result updateUserInformation(HnInformation hnInformation){
        return userService.updateUserInformation(hnInformation);
    }

    @RequestMapping("/query")
    @ResponseBody
    @LogAop(actionName = "用户信息查询",actionType = LogConsts.ACTION_QUERY)
    public Result query(String userInformationName, String userInformationSex, String userInformationPhoneNumber,
                        @RequestParam int page,
                        @RequestParam int limit){
        HnInformation information = new HnInformation();
        information.setUserInformationName(userInformationName);
        information.setUserInformationSex(userInformationSex);
        information.setUserInformationPhoneNumber(userInformationPhoneNumber);
        return userService.query(information,page,limit);
    }

    /**
     * mongodb上传图片
     * @param file
     * @param request
     * @param authentication
     * @return
     */
    @RequestMapping("/uploadMongoDb")
    @ResponseBody
    @LogAop(actionName = "MongoDb头像上传",actionType = LogConsts.ACTION_UPLOAD)
    public Result uploadMongoDb(@RequestParam("file") MultipartFile file, HttpServletRequest request, Authentication authentication){
        return userService.uploadMongoDb(file,request,authentication);
    }

    /**
     * 获取图片
     */
    @GetMapping(value = "/mongoDbShow/{id}", produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
    @LogAop(actionName = "MongoDb查看图片",actionType = LogConsts.ACTION_QUERY)
    public void mongoDbShow(@PathVariable("id") String id, HttpServletResponse response) {
        userService.mongoDbShow(id,response);
    }

    @RequestMapping("/distributeRole")
    @ResponseBody
    @LogAop(actionName = "分配角色",actionType = LogConsts.ACTION_UPDATE)
    public Result distributeRole(HnUser user){
        return userService.distributeRole(user);
    }

    @RequestMapping("/updateUserPassword")
    @ResponseBody
    @LogAop(actionName = "修改密码",actionType = LogConsts.ACTION_UPDATE)
    public Result updateUserPassword(HnUser user, Authentication authentication){
        return userService.updateUserPassword(user,authentication);
    }

    @RequestMapping("/queryEmployee")
    @ResponseBody
    @LogAop(actionName = "用户信息查询",actionType = LogConsts.ACTION_QUERY)
    public Result queryEmployee(String userInformationName, String userInformationSex,
                        @RequestParam int page,
                        @RequestParam int limit){
        HnInformation information = new HnInformation();
        information.setUserInformationName(userInformationName);
        information.setUserInformationSex(userInformationSex);
        return userService.queryEmployee(information,page,limit);
    }

    @RequestMapping("/querySalary")
    @ResponseBody
    @LogAop(actionName = "用户信息查询",actionType = LogConsts.ACTION_QUERY)
    public Result querySalary(String userInformationName, String userInformationSex, String userInformationPhoneNumber,
                        @RequestParam int page,
                        @RequestParam int limit){
        HnInformation information = new HnInformation();
        information.setUserInformationName(userInformationName);
        information.setUserInformationSex(userInformationSex);
        information.setUserInformationPhoneNumber(userInformationPhoneNumber);
        return userService.querySalary(information,page,limit);
    }

    @RequestMapping("/salary")
    @ResponseBody
    @LogAop(actionName = "修改密码",actionType = LogConsts.ACTION_UPDATE)
    public Result salary(Authentication authentication,@RequestParam Integer userId){
        return userService.salary(userId,authentication);
    }
}
